package cn.sptpc.dao.impl;

import cn.sptpc.dao.GroupDao;
import cn.sptpc.entity.java.Department;
import cn.sptpc.entity.java.Group;
import cn.sptpc.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GroupDaoI implements GroupDao {
    private DBUtil dbUtil = DBUtil.getDbUtil();
    /**
     * 插入一条记录
     *
     * @param group
     * @return
     */
    @Override
    public Integer insertGroup(Group group) {
        Integer i = null;
        Connection con = dbUtil.getConnection();
        String sql = "insert into `group` (department_id,`group`) values (?,?)";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setLong(1,group.getDepartmentId());
            pstm.setString(2,group.getGroup());
            i = pstm.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过id更新一条记录
     *
     * @param group
     * @return
     */
    @Override
    public Integer updataGroup(Group group) {
        return null;
    }

    /**
     * 获取行总数
     *
     * @return
     */
    @Override
    public Integer countAllGroup() {
        return null;
    }

    /**
     * 通过id 查询一条记录
     *
     * @param id
     * @return
     */
    @Override
    public Group queryDepartment(int id) {
        Connection con = dbUtil.getConnection();
        String sql = "select * from group where id = ? ";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setInt(1,id);
            ResultSet rs = pstm.executeQuery();
            Group group = new Group();
            while (rs.next()){
                group.setId(rs.getLong("id"));
                group.setDepartmentId(rs.getLong("department_id"));
                group.setGroup(rs.getString("group"));
            }
            return group;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteGroup(int id) {

        return null;
    }

    /**
     * 通过Id查询所有组别
     *
     * @param id
     * @return
     */
    @Override
    public List<Group> findGroupByDepartment(Integer id) {
        Connection con = dbUtil.getConnection();
        String sql = "select * from `group` where department_id = ? ";
        PreparedStatement pstm = null;
        try {
            pstm = con.prepareStatement(sql);
            pstm.setInt(1,id);
            ResultSet rs = pstm.executeQuery();
            List<Group> groups = new ArrayList<>();
            while (rs.next()){
                Group group = new Group();
                group.setId(rs.getLong("id"));
                group.setDepartmentId(rs.getLong("department_id"));
                group.setGroup(rs.getString("group"));
                groups.add(group);
            }
            return groups;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}
